package com.red.basic.service.impl;

import com.red.basic.domain.RedGoods;
import com.red.basic.domain.RedGoodsFolder;
import com.red.basic.mapper.RedGoodsMapper;
import com.red.basic.service.IRedGoodsFolderService;
import com.red.basic.service.IRedGoodsService;
import com.red.common.exception.ServiceException;
import com.red.common.utils.StringUtils;
import com.red.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物资管理Service业务层处理
 * 
 * @author bug2222
 * @date 2024-11-14
 */
@Service
public class RedGoodsServiceImpl implements IRedGoodsService 
{
    @Autowired
    private RedGoodsMapper redGoodsMapper;
    @Autowired
    private IRedGoodsFolderService folderService;

    /**
     * 查询物资管理
     * 
     * @param id 物资管理主键
     * @return 物资管理
     */
    @Override
    public RedGoods selectRedGoodsById(Long id)
    {
        return redGoodsMapper.selectRedGoodsById(id);
    }

    /**
     * 查询物资管理列表
     * 
     * @param redGoods 物资管理
     * @return 物资管理
     */
    @Override
    public List<RedGoods> selectRedGoodsList(RedGoods redGoods)
    {
        return redGoodsMapper.selectRedGoodsList(redGoods);
    }

    /**
     * 新增物资管理
     * 
     * @param redGoods 物资管理
     * @return 结果
     */
    @Override
    public int insertRedGoods(RedGoods redGoods)
    {
        return redGoodsMapper.insertRedGoods(redGoods);
    }

    /**
     * 修改物资管理
     * 
     * @param redGoods 物资管理
     * @return 结果
     */
    @Override
    public int updateRedGoods(RedGoods redGoods)
    {
        return redGoodsMapper.updateRedGoods(redGoods);
    }

    /**
     * 批量删除物资管理
     * 
     * @param ids 需要删除的物资管理主键
     * @return 结果
     */
    @Override
    public int deleteRedGoodsByIds(Long[] ids)
    {
        return redGoodsMapper.deleteRedGoodsByIds(ids);
    }

    /**
     * 删除物资管理信息
     * 
     * @param id 物资管理主键
     * @return 结果
     */
    @Override
    public int deleteRedGoodsById(Long id)
    {
        return redGoodsMapper.deleteRedGoodsById(id);
    }

    @Override
    public List<RedGoodsFolder> getGoodsFolder() {
        List<RedGoodsFolder> goodsFolder = folderService.getRootFolder();
        setChildren(goodsFolder);
        return goodsFolder;
    }

    @Override
    public String importGoods(List<RedGoods> goodsList, boolean updateSupport, String operName) {
        redGoodsMapper.insertRedGoodsBatch(goodsList);
        return "导入成功";
    }

    private void setChildren(List<?> goodsFolderList) {
        goodsFolderList.forEach(item -> {
            if (item instanceof RedGoodsFolder) {
                RedGoodsFolder folder = (RedGoodsFolder) item;
                RedGoodsFolder param = new RedGoodsFolder();
                param.setParentId(folder.getId());
                folder.setChildren(folderService.selectRedGoodsFolderList(param));
                setChildren(folder.getChildren());
            }

        });
    }


}
